//
//  MGCollectionViewCell.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/5/3.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "MGCollectionViewCell.h"

@implementation MGCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self initSubView];
        [self layout];
    }
    return self;
}
- (void)initSubView{
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    self.layer.borderColor  =LINCOLOR.CGColor;
    self.layer.borderWidth = 1;
    [self addSubview:self.nameLabel];
    [self addSubview:self.countLabel];
    [self addSubview:self.deleteButton];
    [self addSubview:self.resetNameButton];
    [self addSubview:self.mergeButton];
    [self addSubview:self.selectedButton];
}
- (void)layout{
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@15);
        make.right.equalTo(@0).offset(-10);
        make.height.equalTo(@30);
    }];
    [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_nameLabel.mas_left);
        make.top.equalTo(self->_nameLabel.mas_bottom).offset(20);
        make.right.equalTo(self->_nameLabel.mas_right);
        make.height.equalTo(@12);
    }];
    CGFloat w = self.frame.size.width/3;
    [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.width.equalTo(@0).offset(w);
        make.height.equalTo(@40);
    }];
    [_resetNameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0).offset(w);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.width.equalTo(@0).offset(w);
        make.height.equalTo(@40);
    }];
    [_mergeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0).offset(w*2+1);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.width.equalTo(@0).offset(w);
        make.height.equalTo(@40);
    }];
    [_selectedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@0).offset(-10);
        make.top.equalTo(@10);
        make.width.equalTo(@20);
        make.height.equalTo(@20);
    }];
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"新概念英语二(三班) Lesson  65";
        label.textColor = SELECTEDCOLOR;
        label.textAlignment = 0;
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:12];
        _nameLabel = label;
    }
    return _nameLabel;
}
- (UILabel *)countLabel{
    if (!_countLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"数量：60";
        label.textColor = TITLECOLOR;
        label.textAlignment = 0;
        label.font = [UIFont systemFontOfSize:12];
        label.attributedText = [PublickFunctionObject getPriceAttribute:label.text and:[UIColor redColor] andChangeBenginString:@"：" andEndString:@"" andSize:13];
        
        _countLabel = label;
    }
    return _countLabel;
}
- (UIButton *)deleteButton{
    if (!_deleteButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"删除" forState:UIControlStateNormal];
        [button setTitleColor:TITLECOLOR forState:0];
        [button addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        _deleteButton  =button;
    }
    return _deleteButton;
}
- (void)deleteButtonClick:(UIButton *)sender{
    self.deleteBlock(self.indexPath);
}
- (UIButton *)resetNameButton{
    if (!_resetNameButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"重命名" forState:UIControlStateNormal];
        [button setTitleColor:TITLECOLOR forState:0];
        [button addTarget:self action:@selector(resetNameButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        _resetNameButton  =button;
    }
    return _resetNameButton;
}
- (void)resetNameButtonClick:(UIButton *)sender{
    self.resetNameBlock(self.indexPath);
}
- (UIButton *)mergeButton{
    if (!_mergeButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"合并" forState:UIControlStateNormal];
        [button setTitleColor:TITLECOLOR forState:0];
        [button addTarget:self action:@selector(mergeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        _mergeButton  =button;
    }
    return _mergeButton;
}
- (void)mergeButtonClick:(UIButton *)sender{
    self.merginBlock(self.indexPath);
}
- (UIButton *)selectedButton{
    if (!_selectedButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"fudao_mergin_normol"] forState:0];
        [button setImage:[UIImage imageNamed:@"fudao_merigin_selected"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(selectedButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        button.selected = NO;
        button.hidden = YES;
        _selectedButton  =button;
    }
    return _selectedButton;
}
- (void)selectedButtonClick:(UIButton *)sender{
    sender.selected = YES;
    if (sender.selected == YES) {
        self.selectedBlock(self.indexPath);
    }
}
- (void)resetSelectedObject{
    _selectedButton.selected = NO;
}
- (void)drawRect:(CGRect)rect{
    CGFloat w = self.frame.size.width/3;
    UIBezierPath *topPath = [[UIBezierPath alloc]init];
    [topPath moveToPoint:CGPointMake(0, 99)];
    [topPath addLineToPoint:CGPointMake(self.frame.size.width, 99)];
    [LINCOLOR set];
    [topPath stroke];
    
    UIBezierPath *leftPath = [[UIBezierPath alloc]init];
    [leftPath moveToPoint:CGPointMake(w-1, 99)];
    [leftPath addLineToPoint:CGPointMake(w-1, 140)];
    [LINCOLOR set];
    [leftPath stroke];
    
    UIBezierPath *rightPath = [[UIBezierPath alloc]init];
    [rightPath moveToPoint:CGPointMake(w*2+1, 99)];
    [rightPath addLineToPoint:CGPointMake(w*2+1, 140)];
    [LINCOLOR set];
    [rightPath stroke];
}
@end
